#define  MAXSIZE 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElemType;

typedef struct {
  ElemType *data;
  int length;
}SeqList;

// void initList(SeqList *L){
//   L->length=0;
// }

SeqList* initList(){
  SeqList *L=(SeqList*)malloc(sizeof(SeqList));
  L->data=(ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
  L->length=0;
  return L;
}

int appendElem(SeqList *L,ElemType e){
  if(L->length>=MAXSIZE){
    printf("顺序表已满\n");
    return 0;
  }
  L->data[L->length++]=e;
  return 1;
} 

int insertElem(SeqList *L,int pos,ElemType e){
  if(pos<=L->length){
    for(int i=L->length-1;i>=pos-1;i--){
      L->data[i+1]=L->data[i];
    }
  }
  L->data[pos-1]=e;
  L->length++;
  return 1;
}

int deleteElem(SeqList *L,int pos,ElemType *e){
  *e=L->data[pos-1];
  if(pos>=L->length){ 
    printf("非法坐标\n");
    return 0;
  }
  for(int i=pos-1;i<L->length;i++){
    L->data[i]=L->data[i+1];
  }
  L->length--;
  return 1;
}

int findElem(SeqList *L,ElemType e){
  for(int i=0;i<L->length;i++){
    if(L->data[i]==e){
      return i+1;
    }
  }
  return 0;
}

void listElem(SeqList *L){
  printf("遍历顺序表元素:\n");
  for(int i=0;i<L->length;i++){
    printf("%d ",L->data[i]);
  }
  printf("\n");
}

int main(){
  //声明一个线性表并初始化
	SeqList *list = initList();
	
	printf("初始化成功，目前长度占用%d\n",list->length);
	printf("目前占用内存%zu字节\n", sizeof(list->data));
	appendElem(list, 88);
	appendElem(list, 67);
	appendElem(list, 40);
	appendElem(list, 8);
	appendElem(list, 23);
	listElem(list);
	insertElem(list, 1, 18);
	listElem(list);
	ElemType delData;
	deleteElem(list, 2, &delData);
	printf("被删除的数据为：%d\n", delData);
	listElem(list);
	printf("%d\n", findElem(list, 40));
	return 0;
}

